package com.zq.多线程设计模式.EventDriven设计模式.EDA同步;

public class EventDispatcherExample {
    /*
     *InputEvent  定义两个属性x和Yy 主要用于在其他channel 中的运算
     * */
    static class InputEvent extends Event {
        private final int x;
        private final int y;

        InputEvent(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getY() {
            return y;
        }

        public int getX() {
            return x;
        }
    }

    /*
     *  用于存放结果Event
     * */
    static class ResultEvent extends Event {
        private final int result;

        ResultEvent(int result) {
            this.result = result;
        }

        public int getResult() {
            return result;
        }
    }

    /*
     *  处理ResultEvent 的handle
     * */
    static class ResultEventHandle implements Channel<ResultEvent> {

        @Override
        public void dispatch(ResultEvent message) {
            System.out.println("The result is : " + message.getResult());
        }
    }
    /*
     * 处理 InputEventHandle 的handle
     * */
    static class InputEventHandle implements Channel<InputEvent> {

        private final EventDispatcher dispatcher;

        InputEventHandle(EventDispatcher dispatcher) {
            this.dispatcher = dispatcher;
        }

        @Override
        public void dispatch(InputEvent message) {
            System.out.printf("X:%d,Y :%d", message.getX(), message.getY());
            int result = message.getX() + message.getY();
            dispatcher.dispatch(new ResultEvent(result));
        }
    }

    public static void main(String[] args) {
        EventDispatcher dispatcher = new EventDispatcher();
        dispatcher.registerChannel(InputEvent.class,new InputEventHandle(dispatcher));
        dispatcher.registerChannel(ResultEvent.class,new ResultEventHandle());
        dispatcher.dispatch(new InputEvent(1,2));


    }

}
